//
//  WYNSString.m
//  label
//
//  Created by 王云 on 16/5/24.
//  Copyright © 2016年 com. All rights reserved.
//

#import "WYNSString.h"
@implementation WYNSString

+ (NSMutableAttributedString *)Att:(NSString *)String rang:(NSString *)rang font:(UIFont *)fon color:(UIColor *)color ParagraphSpacing:(CGFloat )ParagraphSpacing{
    
    NSMutableAttributedString *hintString= [[NSMutableAttributedString alloc]initWithString:String];
    NSRange range1=[[hintString string]rangeOfString:rang];
    [hintString addAttributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:fon} range:range1];
    
    if (ParagraphSpacing != 0) {
        NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setParagraphSpacing:ParagraphSpacing];
        [hintString  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [String length])];
    }

    return hintString;
}


+ (NSMutableAttributedString *)Att:(NSString *)String font:(UIFont*)fon color:(UIColor *)color{
    
     NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:String];
    
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]+$" options:0 error:NULL];
    NSString *result = [regular stringByReplacingMatchesInString:String options:0 range:NSMakeRange(0, [String length]) withTemplate:@""];
   
     NSRange range1=[[hintString string]rangeOfString:result];
    [hintString addAttributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:fon} range:range1];
    
    return hintString;
}

//更改所有的某个颜色
+ (NSMutableAttributedString *)AllAtt:(NSString *)String rang:(NSString *)rang font:(UIFont *)fon color:(UIColor *)color{
    
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc]initWithString:String];
    
    NSString *temp = nil;
    for(int i =0; i < [attributedString length]; i++) {
        temp = [String substringWithRange:NSMakeRange(i, 1)];
        if ([temp isEqualToString:rang]) {
            [attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                             color, NSForegroundColorAttributeName,
                                             fon,NSFontAttributeName, nil]
                                      range:NSMakeRange(i, 1)];
        }
    }
    return attributedString;
}
//更改数组里面所有字体类型的颜色   行间距
+ (NSMutableAttributedString *)AllAtt:(NSString *)String rangArray:(NSArray *)rangArray font:(UIFont *)fon color:(UIColor *)color LineSpacing:(CGFloat)LineSpacing{
    
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc]initWithString:String];
    for (NSString *rang in rangArray) {
        NSString *temp = nil;
        for(int i =0; i < [attributedString length]; i++) {
            temp = [String substringWithRange:NSMakeRange(i, 1)];
            if ([temp isEqualToString:rang]) {
                [attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                 color, NSForegroundColorAttributeName,
                                                 fon,NSFontAttributeName, nil]
                                          range:NSMakeRange(i, 1)];
            }
        }
    }
    NSArray *number = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    for (int i = 0; i < String.length; i ++) {
        //这里的小技巧，每次只截取一个字符的范围
        NSString *a = [String substringWithRange:NSMakeRange(i, 1)];
        //判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符，从而筛选出符合要求的数字字符的范围NSMakeRange
        if ([number containsObject:a]) {
//            [attributedString setAttributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:25],NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(i, 1)];
            [attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(i, 1)];
        }
        
    }
    
    NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle  setLineSpacing:8];
    [attributedString  addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [String length])];

    return attributedString;
}



@end
